********************************************************************************
** 	TITLE: f8_first_stage.do
**
**	PROJECT: IGNITE
** 
**  PURPOSE: Appendix Table X: First-Stage Effects of Court Delays

********************************************************************************
		set sortseed 13

	use "$output_data/estimate.dta", replace

**First Stage
est clear
			//stay ignite_0 for stay outcome measures
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec
	
		foreach v in stay ignite_0{
			if "`v'"=="ignite_0"{
				local ignite fsig
			}	
			if "`v'"=="stay"{
				local ignite fs
			}	
			
			
			forvalues j=0/1{
				if `j'==0{
					local controls " "
				}
				if `j'==1{
					local controls $auxiliary_control
				}

						
						reghdfe `v' interact_proxy_any D_remove_any `controls' D_ignite_proxy if `v'>=0, /// 
							cluster(inmate) a($design_control)
						eststo `ignite'_ceany`j'
						estadd scalar clust e(N_clust): `ignite'_ceany`j'
						sum `v' if e(sample)==1 & D_remove_any==0 &D_ignite_proxy==0
						local meantemp = r(mean)
						local mean: display %8.3f `meantemp' 			
						estadd local meancont"`mean'" : `ignite'_ceany`j'
						
						* get fstats
						qui ivreghdfe D_maj (ignite_0 stay = interact_proxy_any D_remove_any) `controls' D_ignite_proxy ///
								if D_maj>=0, ///
							cluster(inmate) ffirst a($design_control)
						mat SWF=e(first)
						local temp_f1 = SWF[8,1]
						local temp_f2 = SWF[8,2]
						local f1: display %8.3f `temp_f1'
						local f2: display %8.3f `temp_f2'
						estadd local f1 "`f1'" : `ignite'_ceany`j'
						estadd local f2 "`f2'" : `ignite'_ceany`j'
						
						if `j'==0 {
								estadd local postig "Yes": `ignite'_ceany`j'
								estadd local basecont "Yes": `ignite'_ceany`j'
								estadd local demo_case "No": `ignite'_ceany`j'
							}
						if `j'==1  {
								estadd local postig "Yes": `ignite'_ceany`j'
								estadd local basecont "Yes": `ignite'_ceany`j'
								estadd local demo_case "Yes": `ignite'_ceany`j'
							}							
			}
		}	
	
foreach def in any{
		estout fsig_ceany0  fsig_ceany1  fs_ceany0 fs_ceany1    ///
				using "$tables/fs_any_main.tex", style(tex)  ///
				label nobaselevels collabels(none) mlabels(none) /// 
				keep(D_remove_any  interact_proxy_any) ///
				varlabel(D_remove_any "Court Delay"  interact_proxy_any "Court Delay $\times$ Post-IGNITE") ///
				cells(b(star fmt(%8.3f)) se(par fmt(%8.3f))) /// 
				starlevels(* 0.10 ** 0.05 *** 0.01) ///  
				stats(foo meancont f1 f2  basecont demo_case N, ///
				fmt(0 %8.3f %8.3f %8.3f 0 0 %9.0gc) /// 
				labels("\hspace{0.5cm}" "Control Mean" "\emph{F}- Stats.:Any Delay $\times$ Post-IGNITE" "\emph{F}- Stats.:Any Delay" ///
					"Design Controls" "Auxiliary Controls" "Observations"))	replace
				}